package com.nfIt.Dbutils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.time.LocalDateTime;

/**
 * ClossName:GsonUtil
 * Pakage:com.nfIt.jdbcDbutils
 * Description
 *
 * @Author L
 * @Create 2024/3/12 9:04
 * @Version 2023.1.3
 */
public class GsonUtil {
    /**
     * 序列化对象
     *
     * @param object
     * @return JSON字符串 - 对 LocalDateTime 进行了处理
     */
    public static String toJson(Object object){
        if (object == null){
            return "";
        }
        Gson gson = new GsonBuilder()
                // .setDateFormat("yyyy-MM-dd hh:mm:ss")
                // 注册类型适配器
                .registerTypeAdapter(LocalDateTime.class,new LocalDateTimeAdapter())
                //忽略空字段
                .serializeNulls()
                .create();
        return gson.toJson(object);
    }


    public static <T>T fromJson(String json,Class<T> clazz){
        if (json == null || "".equals(json.trim())){
            return null;
        }
        Gson gson = new GsonBuilder()
                //忽略空字段
                .serializeNulls()
                .create();
        return gson.fromJson(json,clazz);
    }








}
